﻿package mortal.game.view.forging
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.guide.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.forging.view.*;
    import mortal.game.view.forging.view.compound.*;
    import mortal.game.view.palyer.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ForgingModule extends BaseWindow
    {
        private var _tabBar:GTabarNew;
        private var _equipDisplaySpr:EquipDisplaySpr;
        private var _backPackSpr:BackPackView;
        private var _tabToClassInfoVec:Vector.<TabToClassInfoData>;
        private var _panelClassVec:Vector.<Class>;
        private var _currentPanel:ForgingPanelBase;
        private var _currSelPage:int = -1;
        private var _isResLoadCompl:Boolean;
        private var _rect3d:Rect3DObject;
        private var _openPanelData:Object;

        public function ForgingModule(param1:ILayer = null)
        {
            this._tabToClassInfoVec = new Vector.<TabToClassInfoData>;
            this._panelClassVec = new Vector.<Class>;
            super(param1);
            setSize(773 + 2, 497 - 3);
            title = Language.getString(41207);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.initPanelClass();
            this.initTabData();
            this._tabBar = UIFactory.gTabBarNew(3, 32, this.getTabDataByRoleLevel(), 768 + 2, 497 - 5, 80, 26, this, this.onTabBarChange, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this._tabBar.name = "AutoGuide_Tabar";
            this._equipDisplaySpr = UICompomentPool.getUICompoment(EquipDisplaySpr, this);
            this._equipDisplaySpr.x = 484;
            this._equipDisplaySpr.y = 70 - 52;
            contentTopOf3DSprite.addChild(this._equipDisplaySpr);
            this.onResLoadCompl();
            this.addEventListener(WindowEvent.POSITIONCHANGE, this.onWinMoveHandler);
            return;
        }// end function

        private function initPanelClass() : void
        {
            this._panelClassVec.push(StrengthenPanel);
            this._panelClassVec.push(EquipRefreshPanel);
            this._panelClassVec.push(EquipColorAdvancePanel);
            this._panelClassVec.push(EquipQualityAdvancePanel);
            this._panelClassVec.push(GemEmbedPanel);
            this._panelClassVec.push(GemStrengthenPanel3);
            this._panelClassVec.push(EquipTransformPanel);
            this._panelClassVec.push(EquipDecomposePanel);
            this._panelClassVec.push(CompoundPanel);
            return;
        }// end function

        private function initTabData() : void
        {
            var _loc_3:TabToClassInfoData = null;
            var _loc_1:* = Language.getArray(40105);
            var _loc_2:int = 0;
            while (_loc_2 < this._panelClassVec.length)
            {
                
                _loc_3 = new TabToClassInfoData();
                _loc_3.tabType = _loc_2;
                _loc_3.tabData = _loc_1[_loc_2];
                _loc_3.panelClass = this._panelClassVec[_loc_2];
                _loc_3.openLevel = GameDefConfig.instance.getForgingPanelOpenLevel(_loc_2);
                this._tabToClassInfoVec.push(_loc_3);
                _loc_2++;
            }
            return;
        }// end function

        private function getTabDataByRoleLevel() : Array
        {
            var _loc_4:TabToClassInfoData = null;
            var _loc_1:Array = [];
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            var _loc_3:int = 0;
            while (_loc_3 < this._tabToClassInfoVec.length)
            {
                
                _loc_4 = this._tabToClassInfoVec[_loc_3];
                if (_loc_4.openLevel <= _loc_2)
                {
                    _loc_1.push(_loc_4.tabData);
                }
                else
                {
                    this._tabToClassInfoVec.splice(_loc_3, 1);
                    _loc_3 = _loc_3 - 1;
                }
                _loc_3++;
            }
            return _loc_1;
        }// end function

        private function onResLoadCompl(param1 = null) : void
        {
            if (param1)
            {
            }
            else
            {
                this._isResLoadCompl = true;
            }
            if (this._isResLoadCompl)
            {
                this.initView();
            }
            return;
        }// end function

        private function initView() : void
        {
            this.onTabBarChange();
            return;
        }// end function

        private function init3dRect() : void
        {
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(11, 59 + 2, 763, 436), this);
            Rect3DManager.instance.windowShowHander(null, this);
            return;
        }// end function

        private function onTabBarChange(event:MuiEvent = null) : void
        {
            var _loc_4:PlayerEquipItem = null;
            var _loc_5:AutoGuideData = null;
            var _loc_6:Boolean = false;
            var _loc_7:Vector.<OtherEquipItem> = null;
            var _loc_8:int = 0;
            var _loc_9:PlayerEquipItem = null;
            var _loc_10:int = 0;
            var _loc_11:ItemData = null;
            var _loc_12:int = 0;
            var _loc_2:* = AutoGuideController._guidingMainId;
            if (_loc_2 >= 27 && _loc_2 <= 30)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStopCurStep, [27, 28, 29, 30]));
            }
            this._currSelPage = Cache.instance.forging.getPageIndexByPanelClass(this._tabToClassInfoVec[this._tabBar.selectedIndex].panelClass);
            if (this._currSelPage == ForgingConst.Page_ItemDecompose)
            {
                if (this._equipDisplaySpr && !this._equipDisplaySpr.isDisposed)
                {
                    this._equipDisplaySpr.dispose();
                }
                if (this._backPackSpr == null)
                {
                    this._backPackSpr = UICompomentPool.getUICompoment(BackPackView);
                    this._backPackSpr.x = 502;
                    this._backPackSpr.y = 58 + 2;
                    this.addChild(this._backPackSpr);
                }
            }
            else if (this._currSelPage == ForgingConst.Page_Compound)
            {
                if (this._equipDisplaySpr && !this._equipDisplaySpr.isDisposed)
                {
                    this._equipDisplaySpr.dispose();
                }
                if (this._backPackSpr && !this._backPackSpr.isDisposed)
                {
                    this._backPackSpr.dispose();
                    this._backPackSpr = null;
                }
            }
            else
            {
                if (this._backPackSpr && !this._backPackSpr.isDisposed)
                {
                    this._backPackSpr.dispose();
                    this._backPackSpr = null;
                }
                if (this._equipDisplaySpr && this._equipDisplaySpr.isDisposed)
                {
                    this._equipDisplaySpr = UICompomentPool.getUICompoment(EquipDisplaySpr);
                    this._equipDisplaySpr.x = 484;
                    this._equipDisplaySpr.y = 70 - 52;
                    this.contentTopOf3DSprite.addChild(this._equipDisplaySpr);
                }
                this._equipDisplaySpr.clear();
                this._equipDisplaySpr.showStrengInfo();
            }
            if (Cache.instance.forging.embedCallBackData != null)
            {
                Cache.instance.forging.embedCallBackData = null;
            }
            if (this._currSelPage == ForgingConst.Page_EquipRefresh)
            {
                this._equipDisplaySpr.updateAllRefreshState();
                _loc_4 = this._equipDisplaySpr.getAvailableEquip();
                if (_loc_4 != null && Cache.instance.autoGuide.isGuidingZBXL())
                {
                    _loc_5 = AutoGuideConfig.instance.getConfigs(["mainId", "stepId"], [27, 3])[0];
                    _loc_5.clickChildName = _loc_4.name;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:27, stepId:2}));
                    Cache.instance.autoGuide._isGuidedZBXL = true;
                }
            }
            else if (this._currSelPage == ForgingConst.Page_QualityUpgrade)
            {
                _loc_4 = this._equipDisplaySpr.getAvailableEquip();
                if (_loc_4 != null && Cache.instance.autoGuide.isGuidingPZTS())
                {
                    _loc_5 = AutoGuideConfig.instance.getConfigs(["mainId", "stepId"], [28, 3])[0];
                    _loc_5.clickChildName = _loc_4.name;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:28, stepId:2}));
                    Cache.instance.autoGuide._isGuidedPZTS = true;
                }
            }
            if (this._currSelPage == ForgingConst.Page_ColorAdvance)
            {
                this._equipDisplaySpr.updateAllColorAdvanceState();
            }
            if (this._currSelPage == ForgingConst.Page_QualityUpgrade)
            {
                this._equipDisplaySpr.updateAllStarAdvanceState();
            }
            if (this._currSelPage == ForgingConst.Page_GemStrengthen)
            {
                this._equipDisplaySpr.updateEquipOperState();
            }
            if (this._currSelPage == ForgingConst.Page_GemEmbed)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGemEmbedInfo));
                if (this._currSelPage == ForgingConst.Page_GemEmbed)
                {
                    this._equipDisplaySpr.updateAllEmbedState();
                    if (Cache.instance.autoGuide.isGuidingBSXQ())
                    {
                        _loc_6 = false;
                        _loc_7 = this._equipDisplaySpr.equipmentList;
                        _loc_8 = 0;
                        while (_loc_8 < _loc_7.length)
                        {
                            
                            _loc_9 = _loc_7[_loc_8];
                            if (_loc_9.isAvailable && EquipmentUtil.isCanGuideEmbed(_loc_9.itemData))
                            {
                                _loc_4 = _loc_9;
                                break;
                            }
                            _loc_8++;
                        }
                        if (_loc_4 != null)
                        {
                            _loc_5 = AutoGuideConfig.instance.getConfigs(["mainId", "stepId"], [29, 3])[0];
                            _loc_5.clickChildName = _loc_4.name;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:29, stepId:2}));
                            Cache.instance.autoGuide._isGuidedBSXQ = true;
                        }
                    }
                }
                else
                {
                    this._equipDisplaySpr.updateAllGemUpgradeState();
                    _loc_6 = false;
                    if (Cache.instance.autoGuide.isGuidingBSQH())
                    {
                        _loc_7 = this._equipDisplaySpr.equipmentList;
                        _loc_10 = 0;
                        _loc_8 = 0;
                        while (_loc_8 < _loc_7.length)
                        {
                            
                            _loc_9 = _loc_7[_loc_8];
                            _loc_11 = _loc_9.itemData;
                            _loc_12 = EquipmentUtil.getFirstEmbedGemIndex(_loc_9.itemData);
                            if (_loc_9.isAvailable && _loc_12 >= 0 && EquipmentUtil.isCanGemUpgrade(_loc_9.itemData))
                            {
                                _loc_10 = _loc_12;
                                _loc_4 = _loc_9;
                                break;
                            }
                            _loc_8++;
                        }
                        if (_loc_4 != null)
                        {
                            _loc_5 = AutoGuideConfig.instance.getConfigs(["mainId", "stepId"], [30, 3])[0];
                            _loc_5.clickChildName = _loc_4.name;
                            _loc_5 = AutoGuideConfig.instance.getConfigs(["mainId", "stepId"], [30, 4])[0];
                            _loc_5.clickChildName = "FrogingModule_Gem" + _loc_10;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:30, stepId:2}));
                            Cache.instance.autoGuide._isGuidedBSQH = true;
                        }
                    }
                }
            }
            var _loc_3:* = UICompomentPool.getUICompoment(this._tabToClassInfoVec[this._tabBar.selectedIndex].panelClass, this);
            if (_loc_3 != this._currentPanel)
            {
                if (this._currentPanel && !this._currentPanel.isDisposed)
                {
                    this._currentPanel.dispose();
                }
                this.contentTopOf3DSprite.addChild(_loc_3);
                this._currentPanel = _loc_3;
                _loc_3.updateUI();
                _loc_3.x = 9;
                _loc_3.y = 57 + 2;
                if (this._equipDisplaySpr && !this._equipDisplaySpr.isDisposed)
                {
                    this._contentTopOf3DSprite.addChild(this._equipDisplaySpr);
                }
            }
            return;
        }// end function

        public function updateEquipForgingInfo(param1:Object) : void
        {
            var _loc_5:PlayerEquipItem = null;
            var _loc_6:EquipUpdateParamInfo = null;
            var _loc_7:PlayerEquipItem = null;
            var _loc_2:* = param1.updateType;
            var _loc_3:* = param1.itemType;
            var _loc_4:* = param1.posType;
            if (!this._equipDisplaySpr.isDisposed)
            {
                _loc_5 = this._equipDisplaySpr.getEquipByType(_loc_3);
                _loc_6 = new EquipUpdateParamInfo();
                switch(this._currSelPage)
                {
                    case ForgingConst.Page_GemEmbed:
                    {
                        _loc_6.isNeedUpdateStrengInfo = false;
                        _loc_6.isNeedUpdateEmbedInfo = true;
                        _loc_6.embedType = ForgingConst.GemPanelType_Embed;
                        break;
                    }
                    case ForgingConst.Page_GemStrengthen:
                    {
                        _loc_6.isNeedUpdateStrengInfo = false;
                        _loc_6.isNeedUpdateEmbedInfo = true;
                        _loc_6.embedType = ForgingConst.GemPanelType_Upgrade;
                        break;
                    }
                    case ForgingConst.Page_EquipRefresh:
                    {
                        _loc_6.isNeedUpdateStrengInfo = true;
                        _loc_6.isNeedUpdateRefreshInfo = true;
                        break;
                    }
                    case ForgingConst.Page_ColorAdvance:
                    {
                        _loc_6.isNeedUpdateStrengInfo = true;
                        _loc_6.isNeedUpdateColorAdvanceInfo = true;
                        break;
                    }
                    case ForgingConst.Page_QualityUpgrade:
                    {
                        _loc_6.isNeedUpdateStrengInfo = true;
                        _loc_6.isNeedUpdateStarAdvanceInfo = true;
                        break;
                    }
                    default:
                    {
                        _loc_6.isNeedUpdateStrengInfo = true;
                        _loc_6.isNeedUpdateEmbedInfo = false;
                        break;
                        break;
                    }
                }
                this._equipDisplaySpr.upDateEquipByType(_loc_3, _loc_6);
                _loc_7 = this._equipDisplaySpr.currSelEquip;
                if (_loc_7 && _loc_7.itemData && _loc_5.itemData && _loc_5.itemData.uid == _loc_7.itemData.uid)
                {
                    if (_loc_2 == UpdateCode.EUpdateCodeBagMove)
                    {
                        this._currentPanel.addForgingEquip(_loc_5);
                    }
                    else if (_loc_4 == EPlayerItemPosType._EPlayerItemPosTypeRole)
                    {
                        this._currentPanel.updateForgingInfo();
                    }
                }
            }
            return;
        }// end function

        public function openForgingPanel(param1:Object) : void
        {
            this._openPanelData = param1;
            if (isHide)
            {
                show();
            }
            if (!this._isResLoadCompl)
            {
                Global.stage.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            }
            else
            {
                this.onOpenPanel(param1);
            }
            return;
        }// end function

        private function onEnterFrame(event:Event) : void
        {
            if (this._isResLoadCompl && !GameController.forging.view.isHide)
            {
                Global.stage.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
                this.onOpenPanel(this._openPanelData);
            }
            return;
        }// end function

        private function onOpenPanel(param1:Object) : void
        {
            var _loc_2:int = 0;
            var _loc_3:ItemData = null;
            var _loc_4:PlayerEquipItem = null;
            var _loc_5:PlayerEquipItem = null;
            if (param1 is int)
            {
                this.openPanelByIndex(param1 as int);
            }
            else if (param1 is String)
            {
                this.openByLabel(param1 as String);
            }
            else
            {
                _loc_2 = param1.pageIndex as int;
                _loc_3 = param1.equipData as ItemData;
                this.openPanelByIndex(_loc_2);
                _loc_4 = this._equipDisplaySpr.getEquipById(_loc_3.uid);
                if (_loc_4)
                {
                    _loc_5 = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
                    if (_loc_5)
                    {
                        _loc_5.isSelect = false;
                    }
                    this._equipDisplaySpr.currSelEquip = _loc_4;
                    _loc_4.setSelEffect(true);
                    this._currentPanel.addForgingEquip(_loc_4);
                }
                if (this._currentPanel is GemStrengthenPanel && param1.holeIndex)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingOnSelGem, param1.holeIndex));
                }
            }
            if (this._currentPanel is StrengthenPanel)
            {
                (this._currentPanel as StrengthenPanel).addBtnFilter();
            }
            return;
        }// end function

        public function openByLabel(param1:String) : void
        {
            var _loc_2:* = this._tabBar.getIndexByLabel(param1);
            if (_loc_2 < 0)
            {
                _loc_2 = 0;
            }
            this.openPanelByIndex(_loc_2);
            return;
        }// end function

        private function openPanelByIndex(param1:int) : void
        {
            var _loc_2:int = 0;
            while (_loc_2 < this._tabToClassInfoVec.length)
            {
                
                if (this._tabToClassInfoVec[_loc_2].tabType == param1)
                {
                    this._tabBar.selectedIndex = _loc_2;
                    this.onTabBarChange();
                    break;
                }
                _loc_2++;
            }
            return;
        }// end function

        public function enableFuncPanel() : void
        {
            this._tabBar.dataProvider = this.getTabDataByRoleLevel();
            this._tabBar.drawNow();
            return;
        }// end function

        public function upDateAllEquip() : void
        {
            this._equipDisplaySpr.upDateAllEquip();
            return;
        }// end function

        public function updateMoneyState() : void
        {
            if (this._currentPanel)
            {
                this._currentPanel.updateMoneyState();
            }
            return;
        }// end function

        public function getPanelClassByPageIndex(param1:int) : Class
        {
            if (param1 < this._panelClassVec.length)
            {
                return this._panelClassVec[param1];
            }
            return null;
        }// end function

        private function onWinMoveHandler(event:Event) : void
        {
            if (GemPackWin.hasInstanceShow())
            {
                GemPackWin.instance.x = this.x - GemPackWin.instance.width - 2;
                GemPackWin.instance.y = this.y;
            }
            return;
        }// end function

        override protected function onMouseMoveHandler(event:MouseEvent) : void
        {
            super.onMouseMoveHandler(event);
            if (BuyGiftItemWin.isViewShow)
            {
                BuyGiftItemWin.instance.x = this.x + this.width + 5;
                BuyGiftItemWin.instance.y = this.y - 3;
            }
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            if (BuyGiftItemWin.isViewShow)
            {
                BuyGiftItemWin.instance.hide();
            }
            return;
        }// end function

        public function get currSelPage() : int
        {
            return this._currSelPage;
        }// end function

        public function set currSelPage(param1:int) : void
        {
            this._currSelPage = param1;
            return;
        }// end function

        public function get currentPanel() : ForgingPanelBase
        {
            return this._currentPanel;
        }// end function

        public function set currentPanel(param1:ForgingPanelBase) : void
        {
            this._currentPanel = param1;
            return;
        }// end function

        public function get equipDisplaySpr() : EquipDisplaySpr
        {
            return this._equipDisplaySpr;
        }// end function

        public function get rect3d() : Rect3DObject
        {
            return this._rect3d;
        }// end function

        public function set rect3d(param1:Rect3DObject) : void
        {
            this._rect3d = param1;
            return;
        }// end function

        public function get isResLoadCompl() : Boolean
        {
            return this._isResLoadCompl;
        }// end function

        public function resetCompoundDataProvider() : void
        {
            if (this._currentPanel is CompoundPanel)
            {
                (this._currentPanel as CompoundPanel).resetDataProvider(false);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._tabBar.name = "";
            this._tabBar.dispose(param1);
            this._tabBar = null;
            if (this._equipDisplaySpr && !this._equipDisplaySpr.isDisposed)
            {
                this._equipDisplaySpr.dispose(param1);
                this._equipDisplaySpr = null;
            }
            if (this._currentPanel && !this._currentPanel.isDisposed)
            {
                this._currentPanel.dispose(param1);
                this._currentPanel = null;
            }
            if (this._backPackSpr && !this._backPackSpr.isDisposed)
            {
                this._backPackSpr.dispose(param1);
                this._backPackSpr = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this._panelClassVec.length = 0;
            this._tabToClassInfoVec.length = 0;
            this._currSelPage = -1;
            this._panelClassVec.length = 0;
            GameController.forging.resetCurrStrengTipData();
            this.removeEventListener(WindowEvent.POSITIONCHANGE, this.onWinMoveHandler);
            super.disposeImpl(param1);
            return;
        }// end function

    }
}
